$(function () {
  var commonHost = 'http://t.scsj.net.cn/admin'
  // 获取url中code值
  var buyerId = $.cookie('uid')
  var goodsId = $.cookie('goodsId')
  var userName = $.cookie('userName')
  var tk = $.cookie('tk')
  $.ajax({
    type: 'post',
    url: commonHost + '/index.php/my/get_default_address',
    data: {tk: tk},
    success: function (data) {
      data = JSON.parse(data);
      if (data.ret == 1) {
        $.cookie('addressId', data.data.id, {
          path: '/',
          expires: 30
        })
        var addressId = data.data.id;
        var address = template('address', data.data);
        $('.address').html(address);
        // 获取购物车信息
        $.ajax({
          type: 'post',
          url: commonHost + '/index.php/cart/my_cart_list',
          data: {tk: $.cookie('tk'),storeId: $.cookie('storeId')},
          success: function (data) {
            data = JSON.parse(data)
            if (data.ret == 1) {
              if (data.data) {
                var goods = data.data[0].goodsList[0];
                var html = template('product', goods);
                $('.product').html(html);
                $('.order h3').text(data.data[0].storeName);
                var freight = parseFloat(data.data[0].delivery) + parseFloat(goods.expFee * goods.num)
                $('.store').attr('data-freight', freight);
                var proPrice = goods.price * goods.num;
                $('.proPrice').text('￥' + proPrice);
                $('.amount').text('￥' + (freight + proPrice))
                $('del').text('￥' + data.data[0].originPrice)
                $('.freight').text('￥' + freight)
                $('input[type="radio"]').change(function () {
                  if ($('.store').prop('checked')) {
                    $('.freight').text('￥' + freight);
                    $('.amount').text('￥' + (freight + proPrice))
                  }else {
                    $('.freight').text('￥0');
                    $(".amount").text("￥"+proPrice);
                  }
                })
              
                var dispatchType = 1;
                // 点击提交订单
                $('.btn').click(function () {
                  if ($('.personal').prop('checked')) {
                    freight = 0;
                    dispatchType = 2;
                  }else if ($('.store').prop('checked')) {
                    freight = $('.store').attr('data-freight')
                    dispatchType = 1;
                  }
                  var storeId = data.data[0].storeId
                  var storeName = data.data[0].storeName
                  var orderAmount = proPrice + freight
                  var num = goods.num
                  var orderMessage = $('.words').val();
                  var dataJson = {
                    orderPrice: orderAmount,
                    order: [
                      {
                        storeId: storeId,
                        storeName: storeName,
                        buyerId: buyerId,
                        buyerName: userName,
                        goodsAmount: proPrice,
                        shippingFee: freight,
                        orderAmount: orderAmount,
                        orderFrom: '1', // 订单来源 web或者mobile
                        dispatchType: dispatchType, // 自提或商家配送1、配送2、自提
                        orderCommon: {
                          orderMessage: orderMessage,
                          invoiceInfo: {},
                          addressId: addressId,
                          invoiceType: ''
                        },
                        orderGoods: [
                          {
                            goodsId: goodsId,
                            num: num,
                            buyerId: buyerId
                          }
                        ]
                      }
                    ]
                  }
                  // 生成订单请求
                  $.ajax({
                    type: 'post',
                    url: commonHost + '/index.php/mall/generate_order',
                    data: dataJson,
                    success: function (data) {
                      data = JSON.parse(data)
                      if (data.ret == 1) {
                        location.href = './pay.html?orderSn=' + data.data.orderSn+"&price="+data.data.price;
                      }else {
                        $('body').toast({
                          content: data.msg
                        })
                      }
                    },error: function () {
                      $('body').toast({
                        content: "系统错误"
                      })
                    }
                  })
                })
              }
            }else {
              $('body').toast({
                content: data.msg
              })
            }
          },error: function () {
            $('body').toast({
              content: "系统错误"
            })
          }
        })
      }else {
        $('body').toast({
          content: data.msg
        })
      }
    },error: function () {
      $('body').toast({
        content: "系统错误"
      })
    }
  })

  $('input[type="radio"]').change(function () {
    var proPrice = parseFloat($(".proPrice").text());
    if ($('.store').prop('checked')) {
      var freight = parseFloat($('.store').attr('data-freight'));
      $('.freight').text('￥' + freight);
      $(".amount").text("￥"+(proPrice+freight));
    }else {
      $('.freight').text('￥0');
      $(".amount").text("￥"+proPrice);
    }
  })

  $('.address').click(function () {
    location.href = './address.html'
  })
})
